<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">远程开门</block>
		</cu-custom>

		<view class="cu-bar bg-white solid-bottom margin-top">
			<view class="action">
				<text class="cuIcon-title text-orange "></text>大门管理
			</view>
		</view>

		<view class="cu-list menu sm-border margin-top">
			<view v-for="(item,index) in deviceList" class="cu-item">
				<view class="content">
					<text class="cuIcon-locationfill text-green"></text>
					<text class="text-grey">{{item.addrname}}</text>
				</view>
				<view class="action">
					<view :class="item.online=='Y'?'bg-green':'bg-red'" class="cu-tag round light margin-right-xs">{{item.online=='Y'?'在线':'不在线'}}</view>
					<button type="" :disabled="item.online!='Y'" @click="ShowOpenDoor(index)" class="cu-btn round bg-green shadow">
						<text class="cuIcon-lock"></text>开门
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				deviceList: []
			}
		},
		onLoad() {
			this.getDeviceList()
		},
		methods: {
			getDeviceList() {
				// var this
				uni.showLoading({
					title: '加载中'
				})
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_DEVICE_LIST,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						orgcode: getApp().globalData.orgcode
					},
					success: (res) => {
						var result = res.data
						this.deviceList = result.data
						console.log(this.deviceList)
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			ShowOpenDoor(index) {
				let door = this.deviceList[index]
				uni.showModal({
					title: '提示',
					content: '即将打开大门"' + door.addrname + '",请确认你的操作',
					success: (res) => {
						if (res.confirm) {
							this.openDoor(door.deviceno)
						}
					}
				})
			},
			openDoor(deviceno) {
				uni.showLoading({
					title: '正在开门'
				})
				uni.request({
					url: this.$constant.BASEURL + this.$constant.REMOTE_OPEN_DOOR,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						unitno: getApp().globalData.orgcode,
						deviceid:deviceno
					},
					success: (res) => {
						var result = res.data
						if(result.code == 0){
							uni.showToast({
								title:'开门请求成功'
							})
						}else{
							uni.showToast({
								title:'开门请求失败',
								icon:'none'
							})
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>

<style>

</style>
